/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/9/5 11:06:15
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;

namespace LEARNREN.COM.UI.LabelProviders
{
    abstract class ListLabelBase : AbstractLabelProvider
    {
        public ListLabelBase(string label)
            : base(label)
        {

        }

        public override abstract string GetHtml();

        /// <summary>
        /// 获得分页html
        /// </summary>
        /// <param name="pageindex">当前页码</param>
        /// <param name="pagesize">页面大小</param>
        /// <param name="total">记录总数</param>
        /// <returns></returns>
        protected string GetPager(int pageindex, int pagesize, int totalcount)
        {
            int pagecount = (int)Math.Ceiling(totalcount / (double)pagesize);

            if (pageindex < 1)
            {
                pageindex = 1;
            }
            if (pageindex > pagecount)
            {
                pageindex = pagecount;
            }

            Catalogue c = null;
            if (this.Context.State is Catalogue)
            {
                c = this.Context.State as Catalogue;
            }
            else
            {
                c = this.GroupLogicFactory.CatalogueLogic.GetByIdFromCache(this.Context.CatalogueID);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<div class=\"showpages\">");

            sb.Append("<span class=\"pageinfo\">");
            sb.AppendFormat("共{0}条，每页{1}条,页次：{2}/{3}", totalcount, pagesize, pageindex, pagecount);
            sb.Append("</span>");
            sb.Append("<span class=\"firstpage\">");
            sb.AppendFormat("<a href=\"{0}\">首页</a>", this.GroupLogicFactory.CatalogueLogic.GetCatalogueURL(c, 1));
            sb.Append("</span>");
            sb.Append("<span class=\"prepage\">");
            sb.AppendFormat("<a href=\"{0}\">上一页</a>", this.GroupLogicFactory.CatalogueLogic.GetCatalogueURL(c, pageindex - 1));
            sb.Append("</span>");
            sb.Append("<span class=\"nextpage\">");
            sb.AppendFormat("<a href=\"{0}\">下一页</a>", this.GroupLogicFactory.CatalogueLogic.GetCatalogueURL(c, pageindex + 1));
            sb.Append("</span>");
            sb.Append("<span class=\"lastpage\">");
            sb.AppendFormat("<a href=\"{0}\">尾页</a>", this.GroupLogicFactory.CatalogueLogic.GetCatalogueURL(c, pagecount));
            sb.Append("</span>");
            sb.Append("<span class=\"jumppage\">");
            sb.AppendLine("<span style=\"vertical-align: middle\">跳转到第</span>");
            sb.AppendLine("<select style=\"vertical-align: middle\" onChange=\"window.location=this.options[this.selectedIndex].value\">");
            for (int i = 1; i <= pagecount; i++)
            {
                string option = string.Format("<option value=\"{0}\"{1}>{2}</option>", this.GroupLogicFactory.CatalogueLogic.GetCatalogueURL(c, i), i == pageindex ? "selected" : string.Empty, i);
                sb.AppendLine(option);
            }
            sb.AppendLine("</select>");
            sb.AppendLine("<span style=\"vertical-align: middle\">页</span>");
            sb.Append("</span>");

            sb.AppendLine("</div>");
            return sb.ToString();
        }
    }
}
